﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class State_List : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            getState();
        }
    }

    #region BindData

    private void getState()
    {
        State state = new State();
        List<State> states = new List<State>();
        state = new State(_dataObject);

        states = state.GetAllState();

        if (states != null)
        {
            rptState.DataSource = states;
            rptState.DataBind();
        }
    }
    #endregion

    #region Webmethod for update and delete
    [System.Web.Services.WebMethod]
    public static string Update(string name, string id)
    {
        State state = new State();
        IDataRepository _dataObject = new DataRepository();
        state = new State(_dataObject);
        state.Name = name;
        state.CountryId = (Int32)EnumClass.Enums.Country.India;
        if (id == "")
            state.Id = 0;
        else
            state.Id = Convert.ToInt32(id);
        state.GetStateByName();

        if (id != "")
        {
            if (state.Id != Convert.ToInt32(id) && state.Id != 0)
            {
                return "0";
            }
            else
            {
                state.IsDeleted = false;
                state.Update();
                return state.Id.ToString();
            }
        }
        else
        {
            if (state.Id > 0)
            {
                return "0";
            }
            else
            {
                state.CreatedDate = System.DateTime.Now;
                state.Create();
                return state.Id.ToString();
            }
        }
    }
    [System.Web.Services.WebMethod]
    public static string Delete(string id)
    {
        try
        {
            FoneBuddy.LinqModel.State state = new State();
            IDataRepository _dataObject = new DataRepository();
            state = new State(_dataObject);
            state.Id = Convert.ToInt32(id);
            state.Delete();
            return "Deleted";
        }
        catch (Exception ex)
        {
            return "error";
        }

    }
    #endregion
}